package calvin.study.redis;

import java.util.LinkedHashMap;
import java.util.Map.Entry;

//LRU算法实现_1
public class LruTest01<K, V> extends LinkedHashMap<K, V> {

	private static final long serialVersionUID = 1910829605964087790L;

	private int capacity; // 缓存坑位

	public LruTest01(int capacity, boolean accessOrder) {
		super(capacity, 0.75F, accessOrder);
		this.capacity = capacity;
	}

	@Override
	protected boolean removeEldestEntry(Entry<K, V> eldest) {
		return super.size() > capacity;
	}

	public static void main(String[] args) {

		LruTest01 lruCacheDemo = new LruTest01(3, false);

		lruCacheDemo.put(1, "a");
		lruCacheDemo.put(2, "b");
		lruCacheDemo.put(3, "c");
		System.out.println(lruCacheDemo.keySet());

		lruCacheDemo.put(4, "d");
		System.out.println(lruCacheDemo.keySet());

		lruCacheDemo.put(3, "c");
		System.out.println(lruCacheDemo.keySet());
		lruCacheDemo.put(3, "c");
		System.out.println(lruCacheDemo.keySet());
		lruCacheDemo.put(3, "c");
		System.out.println(lruCacheDemo.keySet());
		lruCacheDemo.put(5, "x");
		System.out.println(lruCacheDemo.keySet());
	}
}
